﻿(function ($) {
    app.modals.CreateOrEditModal = function () {

        var _courseService = abp.services.app.course;
        var _modalManager;
        var _$form = null;

        this.init = function (modalManager) {
            _modalManager = modalManager;

            _$form = _modalManager.getModal().find('form[name=ObjectForm]');
            _$form.validate();
        };

        this.save = function () {
            if (!_$form.valid()) {
                return;
            }

            var domainObject = _$form.serializeFormToObject();

            _modalManager.setBusy(true);
            _courseService.createOrUpdateObject({
                course: domainObject
            }).done(function (result) {
                abp.notify.info(app.localize('SavedSuccessfully'));
                _modalManager.setResult(result);
                _modalManager.close();
                abp.event.trigger('app.CreateOrEditModalSaved', result);
            }).always(function () {
                _modalManager.setBusy(false);
            });
        };
    };
    //自动生成课程名称
    //if ($("#IsAutoCreateName").is(":checked"))
    //if ($("input[name='IsAutoCreateName'][value='IsAutoCreateName']").attr("checked", "true"))
    //{
    //    $("#GradeGUID").change(function () {
    //        alert($("#GradeGUID option:checked").text());
    //        $("#CourseName").val($("#GradeGUID option:checked").text());
    //    })
    //}
    var _courseName = "";
    //年级
    $("#GradeGUID").change(function () {
        if ($("#IsAutoCreateName").is(":checked")) {
            //$("input[name='CourseName']").each(function () {
            //    _courseName += $(this).val() + ',';
            //})
            _courseName += $("#GradeGUID option:checked").text()
            $("#CourseName").val(_courseName);
        }
    })
    //科目
    $("#SubjectGUID").change(function () {
        if ($("#IsAutoCreateName").is(":checked")) {
            _courseName += $("#SubjectGUID option:checked").text()
            $("#CourseName").val(_courseName);
        }
    })
    //类型
    $("#SubjectTypeGUID").change(function () {
        if ($("#IsAutoCreateName").is(":checked")) {
            _courseName += $("#SubjectTypeGUID option:checked").text()
            $("#CourseName").val(_courseName);
        }
    })
    
    //计费方式切换
    $("#ChargeTypeList").change(function () {
        if ($("#ChargeTypeList").val() == "1") {
            $("#PriceUnits").html("元/小时");
        }
        else if ($("#ChargeTypeList").val() == "2") {
            $("#PriceUnits").html("元/次");
        }
    })

})(jQuery);